package com.zl.task.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zl.task.dao.ServiceEnterprisedMapper;
import com.zl.task.dao.ServiceMechanismMapper;
import com.zl.task.dao.ServiceServerMapper;
import com.zl.task.dto.MechanismDTO;
import com.zl.task.pojo.ServiceEnterprised;
import com.zl.task.pojo.ServiceMechanism;
import com.zl.task.pojo.ServiceServer;
import com.zl.task.service.IServiceMechanismService;
import com.zl.task.service.IServiceToken;
import com.zl.task.util.CheckMessage;
import com.zl.task.vo.MechanismLoginVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import static com.zl.task.commom.ServiceCode.ENTERPRISE_TYPE;
import static com.zl.task.commom.ServiceCode.MECHANISM_TYPE;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yinlingeng
 * @since 2021-07-23
 */
@Service
public class ServiceMechanismServiceImpl extends ServiceImpl<ServiceMechanismMapper, ServiceMechanism> implements IServiceMechanismService {

    @Resource
    ServiceServerMapper serverMapper;
    @Autowired
    private IServiceToken serviceToken;
    @Resource
    ServiceEnterprisedMapper enterpriseMapper;


    /**
     * 机构用户登录
     *
     * @param userName -
     * @param password -
     * @return MechanismLoginVO
     */
    @Override
    public MechanismLoginVO getMechanism(String userName, String password) throws Exception {
        ServiceMechanism serviceMechanism = baseMapper.selectOne(Wrappers.<ServiceMechanism>lambdaQuery()
                .eq(ServiceMechanism::getMechanismPhone, userName)
                .eq(ServiceMechanism::getMechanismPassword, password));
        if (serviceMechanism != null) {
            String token = serviceToken.getToken(serviceMechanism.getMechanismId().toString(), serviceMechanism.getMechanismPassword());
            MechanismLoginVO mechanismLoginVO = new MechanismLoginVO();
            mechanismLoginVO.setMechanismId(serviceMechanism.getMechanismId().toString());
            mechanismLoginVO.setMechanismName(serviceMechanism.getMechanismName());
            mechanismLoginVO.setMechanismContacts(serviceMechanism.getMechanismContacts());
            mechanismLoginVO.setMechanismPhone(serviceMechanism.getMechanismPhone());
            mechanismLoginVO.setToken(token);
            String[] split = serviceMechanism.getMechanismType().split(",");
            Set<String> collect = Arrays.stream(split).collect(Collectors.toSet());
            List<ServiceServer> serviceServers = serverMapper.selectList(Wrappers.<ServiceServer>lambdaQuery()
                    .in(ServiceServer::getServerId, collect));
            mechanismLoginVO.setMechanismType(serviceServers);
            return mechanismLoginVO;
        }
        throw new Exception("登录失败，账号或密码错误");
    }

    /**
     * 机构用户信息维护（修改）
     *
     * @return Boolean
     */
    @Override
    public Boolean updateMechanism(MechanismDTO mechanismDTO) throws Exception {
        ServiceMechanism serviceMechanism = new ServiceMechanism();
        serviceMechanism.setMechanismId(Integer.valueOf(mechanismDTO.getMechanismId()).longValue());
        serviceMechanism.setMechanismName(mechanismDTO.getMechanismName());
        serviceMechanism.setMechanismContacts(mechanismDTO.getMechanismContacts());
        serviceMechanism.setMechanismPhone(CheckMessage.checkPhone(mechanismDTO.getMechanismPhone()));
        serviceMechanism.setMechanismType(mechanismDTO.getMechanismType());
        if (baseMapper.updateById(serviceMechanism) > 0) {
            return true;
        }
        throw new Exception("修改失败");
    }

    /**
     * 根据id查询用户
     *
     * @param id -
     * @return ServiceMechanism
     */
    @Override
    public ServiceMechanism getMenchanismById(String id) {
        return baseMapper.selectOne(Wrappers.<ServiceMechanism>lambdaQuery()
                .eq(ServiceMechanism::getMechanismId, id));
    }

    /**
     * 密码修改
     *
     * @param newPassword -
     * @return Boolean
     */
    @Override
    public Boolean updatePassword(String type, Long id, String password, String newPassword) throws Exception {
        if (CheckMessage.isLetterDigit(newPassword)) {
            if (ENTERPRISE_TYPE.equals(type)) {
                if (enterpriseMapper.selectCount(Wrappers.<ServiceEnterprised>lambdaQuery()
                        .eq(ServiceEnterprised::getEnterprisedId, id)
                        .eq(ServiceEnterprised::getEnterprisePassword, password)) > 0) {
                    ServiceEnterprised serviceEnterprised = new ServiceEnterprised();
                    serviceEnterprised.setEnterprisedId(id);
                    serviceEnterprised.setEnterprisePassword(newPassword);
                    if (enterpriseMapper.updateById(serviceEnterprised) > 0) {
                        return true;
                    }else {
                        throw new Exception("原密码错误，请重新输入");
                    }
                }
            } else if (MECHANISM_TYPE.equals(type)) {
                if (baseMapper.selectCount(Wrappers.<ServiceMechanism>lambdaQuery()
                        .eq(ServiceMechanism::getMechanismId, id)
                        .eq(ServiceMechanism::getMechanismPassword, password)) > 0) {
                    ServiceMechanism serviceMechanism = new ServiceMechanism();
                    serviceMechanism.setMechanismId(id);
                    serviceMechanism.setMechanismPassword(newPassword);
                    if (baseMapper.updateById(serviceMechanism) > 0) {
                        return true;
                    }
                }else {
                    throw new Exception("原密码错误，请重新输入");
                }

            }
        }
            throw new Exception("新密码格式错误：包含大小写字母及数字且在6-12位");
    }

    /**
     * 所有机构用户
     *
     * @return List<ServiceMechanism>
     */
    @Override
    public List<ServiceMechanism> listMechanism() {
        return baseMapper.selectList(Wrappers.<ServiceMechanism>lambdaQuery());
    }


}
